<?php

    require_once dirname(dirname(__FILE__)) . '/config/config.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_date.php';

    class dao_transaction
    {

        //Methods
        public function __construct()
        {

        }

        public function get_all()
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT * "
                . "FROM tbl_transaction ";
            $result = mysqli_query($con, $query)
            or die("Query fail: " . mysqli_error());

            $list = array();
            while ($row = mysqli_fetch_array($result)) {
                array_push($list, $row);
            }
            $db->close_connect();

            return $list;
        }

        public function save($id, $created_date, $user_id,
                             $amount, $method_id, $card_number,
                             $action_id, $description)
        {
            $db  = new connection();
            $con = $db->open_connect();

            if ($id == 0) {
                $query = "INSERT INTO tbl_transaction(created_date, user_id, amount, method_id, card_number, action_id, description) VALUES (
                '" . $created_date . "'," . $user_id . "," . $amount . "," . $method_id . ",'" . $card_number . "'," . $action_id . ",'" . $description . "')";
            } else {
                $query = "UPDATE tbl_transaction SET "
                    . "created_date = '" . $created_date . "',"
                    . "user_id = " . $user_id . ","
                    . "amount = " . $amount . ","
                    . "method_id = " . $method_id . ","
                    . "card_number = '" . $card_number . "',"
                    . "action_id = " . $action_id . ","
                    . "description = '" . $description . "' "
                    . "WHERE id = " . $id;
            }
            mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

            $db->close_connect();

            return TRUE;
        }

        public function get_by_id($id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT * "
                . "FROM tbl_transaction "
                . "WHERE id = " . $id;
            $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

            $row = mysqli_fetch_array($result);

            $db->close_connect();

            return $row;
        }

        public function delete($id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "DELETE FROM tbl_transaction WHERE id = " . $id;
            mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

            $db->close_connect();

            return TRUE;
        }

        /**
         * @param $user_id
         * @param $description
         * @param $action_id (action_id = 3: Spend, action_id = 4: Receive)
         * @param $amount
         *
         * @return bool
         */
        public function insert_transaction_history($user_id, $description, $action_id, $amount, $method_id, $card_number, $transaction_email)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "INSERT INTO tbl_transaction_history(user_id, date, description, action_id, amount, method_id, card_number, transaction_email) VALUES ($user_id,'" . lib_date::get_now() . "', '$description', $action_id, $amount, $method_id, '$card_number', '$transaction_email')";
            mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

            $db->close_connect();

            return TRUE;
        }

        public function get_list_transaction_of_user($user_id)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "SELECT * FROM tbl_transaction_history WHERE user_id = $user_id";
            $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());
            $list = array();

            while ($row = mysqli_fetch_array($result)) {
                array_push($list, $row);
            }
            $db->close_connect();

            return $list;
        }
    }
